#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<queue>
#include<vector>

using namespace std;

class Solution {
    typedef pair<int, int> PII;
public:
    void solve(vector<vector<char>>& board) {
        int m = board.size(), n = board[0].size();
        int dr[] = { -1,1,0,0 }, dc[] = { 0,0,-1,1 };
        vector<vector<bool>> vis(m, vector<bool>(n, 0));
        queue<PII> q;

        auto bfs = [&](int sr, int sc)->void {
            vis[sr][sc] = 1;
            q.push({ sr, sc });
            while (!q.empty()) {
                auto [r, c] = q.front();
                q.pop();
                board[r][c] = 'P';
                for (int i = 0; i < 4; i++) {
                    int nr = r + dr[i], nc = c + dc[i];
                    if (nr >= 0 && nr < m && nc >= 0 && nc < n && !vis[nr][nc] && board[nr][nc] == 'O') {
                        q.push({ nr, nc });
                        vis[nr][nc] = 1;
                    }
                }
            }
            };

        for (int i = 0; i < m; i++) {
            if (board[i][0] == 'O' && !vis[i][0])
                bfs(i, 0);
        }
        for (int i = 0; i < m; i++) {
            if (board[i][n - 1] == 'O' && !vis[i][n - 1])
                bfs(i, n - 1);
        }
        for (int j = 0; j < n; j++) {
            if (board[0][j] == 'O' && !vis[0][j])
                bfs(0, j);
        }
        for (int j = 0; j < n; j++) {
            if (board[m - 1][j] == 'O' && !vis[m - 1][j])
                bfs(m - 1, j);
        }

        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (board[i][j] == 'P') {
                    board[i][j] = 'O';
                }
                else {
                    board[i][j] = 'X';
                }
            }
        }
    }
};